import os
import json
import requests
import time
from datetime import datetime

# 设置常量密钥和APIHost
API_KEY = "your_api_key_here"  # 替换为您的实际API密钥
API_HOST = "api.qweather.com"  # 替换为您的实际API主机

GITURL = "https://gitee.com/wzyaeu/PCLmainpage-Weweather/raw/main"  # git仓库路径

# 确保weather-forecast目录存在
if not os.path.exists("weather-forecast"):
    os.makedirs("weather-forecast")

def read_template_file(filename):
    """从文件中读取模板内容"""
    with open(filename, "r", encoding="utf-8") as f:
        return f.read()

def load_city_data():
    """从citydata.json加载城市数据"""
    with open("citydata.json", "r", encoding="utf-8") as f:
        return json.load(f)

def fetch_weather_data(location):
    """获取指定位置的天气数据"""
    if True :
        return {"code":"200","updateTime":"2025-07-14T21:03+08:00","fxLink":"https://www.qweather.com/weather/beijing-101010100.html","daily":[{"fxDate":"2025-07-14","sunrise":"04:58","sunset":"19:44","moonrise":"21:53","moonset":"08:11","moonPhase":"亏凸月","moonPhaseIcon":"805","tempMax":"35","tempMin":"23","iconDay":"101","textDay":"多云","iconNight":"302","textNight":"雷阵雨","wind360Day":"180","windDirDay":"南风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"180","windDirNight":"南风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"53","precip":"0.7","pressure":"991","vis":"25","cloud":"60","uvIndex":"10"},{"fxDate":"2025-07-15","sunrise":"04:59","sunset":"19:43","moonrise":"22:18","moonset":"09:22","moonPhase":"亏凸月","moonPhaseIcon":"805","tempMax":"29","tempMin":"23","iconDay":"302","textDay":"雷阵雨","iconNight":"151","textNight":"多云","wind360Day":"90","windDirDay":"东风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"45","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"73","precip":"7.8","pressure":"991","vis":"24","cloud":"86","uvIndex":"9"},{"fxDate":"2025-07-16","sunrise":"05:00","sunset":"19:42","moonrise":"22:42","moonset":"10:35","moonPhase":"亏凸月","moonPhaseIcon":"805","tempMax":"32","tempMin":"23","iconDay":"101","textDay":"多云","iconNight":"151","textNight":"多云","wind360Day":"45","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"0","windDirNight":"北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"60","precip":"0.5","pressure":"997","vis":"25","cloud":"42","uvIndex":"10"},{"fxDate":"2025-07-17","sunrise":"05:00","sunset":"19:42","moonrise":"23:07","moonset":"11:48","moonPhase":"亏凸月","moonPhaseIcon":"805","tempMax":"35","tempMin":"25","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"90","windDirDay":"东风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"225","windDirNight":"西南风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"54","precip":"0.0","pressure":"995","vis":"25","cloud":"1","uvIndex":"10"},{"fxDate":"2025-07-18","sunrise":"05:01","sunset":"19:41","moonrise":"23:37","moonset":"13:04","moonPhase":"下弦月","moonPhaseIcon":"806","tempMax":"37","tempMin":"27","iconDay":"100","textDay":"晴","iconNight":"151","textNight":"多云","wind360Day":"180","windDirDay":"南风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"180","windDirNight":"南风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"66","precip":"0.0","pressure":"995","vis":"24","cloud":"3","uvIndex":"10"},{"fxDate":"2025-07-19","sunrise":"05:02","sunset":"19:41","moonrise":"","moonset":"14:21","moonPhase":"残月","moonPhaseIcon":"807","tempMax":"36","tempMin":"26","iconDay":"104","textDay":"阴","iconNight":"104","textNight":"阴","wind360Day":"180","windDirDay":"南风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"45","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"86","precip":"0.0","pressure":"998","vis":"15","cloud":"25","uvIndex":"4"},{"fxDate":"2025-07-20","sunrise":"05:03","sunset":"19:40","moonrise":"00:11","moonset":"15:40","moonPhase":"残月","moonPhaseIcon":"807","tempMax":"31","tempMin":"24","iconDay":"302","textDay":"雷阵雨","iconNight":"302","textNight":"雷阵雨","wind360Day":"45","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"45","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"93","precip":"7.5","pressure":"999","vis":"22","cloud":"62","uvIndex":"3"},{"fxDate":"2025-07-21","sunrise":"05:04","sunset":"19:39","moonrise":"00:53","moonset":"16:57","moonPhase":"残月","moonPhaseIcon":"807","tempMax":"32","tempMin":"22","iconDay":"101","textDay":"多云","iconNight":"305","textNight":"小雨","wind360Day":"45","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"0","windDirNight":"北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"90","precip":"6.1","pressure":"999","vis":"23","cloud":"61","uvIndex":"10"},{"fxDate":"2025-07-22","sunrise":"05:04","sunset":"19:38","moonrise":"01:45","moonset":"18:04","moonPhase":"残月","moonPhaseIcon":"807","tempMax":"30","tempMin":"23","iconDay":"100","textDay":"晴","iconNight":"305","textNight":"小雨","wind360Day":"45","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"45","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"94","precip":"0.0","pressure":"1001","vis":"21","cloud":"7","uvIndex":"10"},{"fxDate":"2025-07-23","sunrise":"05:05","sunset":"19:38","moonrise":"02:47","moonset":"19:02","moonPhase":"残月","moonPhaseIcon":"807","tempMax":"27","tempMin":"23","iconDay":"305","textDay":"小雨","iconNight":"305","textNight":"小雨","wind360Day":"45","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"90","windDirNight":"东风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"76","precip":"0.0","pressure":"999","vis":"25","cloud":"2","uvIndex":"10"},{"fxDate":"2025-07-24","sunrise":"05:06","sunset":"19:37","moonrise":"03:56","moonset":"19:46","moonPhase":"新月","moonPhaseIcon":"800","tempMax":"37","tempMin":"23","iconDay":"305","textDay":"小雨","iconNight":"305","textNight":"小雨","wind360Day":"315","windDirDay":"西北风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"90","windDirNight":"东风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"80","precip":"1.1","pressure":"998","vis":"23","cloud":"43","uvIndex":"9"},{"fxDate":"2025-07-25","sunrise":"05:07","sunset":"19:36","moonrise":"05:09","moonset":"20:21","moonPhase":"蛾眉月","moonPhaseIcon":"801","tempMax":"32","tempMin":"23","iconDay":"305","textDay":"小雨","iconNight":"305","textNight":"小雨","wind360Day":"90","windDirDay":"东风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"0","windDirNight":"北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"71","precip":"0.8","pressure":"998","vis":"25","cloud":"55","uvIndex":"10"},{"fxDate":"2025-07-26","sunrise":"05:08","sunset":"19:35","moonrise":"06:22","moonset":"20:50","moonPhase":"蛾眉月","moonPhaseIcon":"801","tempMax":"35","tempMin":"23","iconDay":"305","textDay":"小雨","iconNight":"305","textNight":"小雨","wind360Day":"90","windDirDay":"东风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"0","windDirNight":"北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"74","precip":"0.2","pressure":"993","vis":"24","cloud":"40","uvIndex":"10"},{"fxDate":"2025-07-27","sunrise":"05:09","sunset":"19:34","moonrise":"07:31","moonset":"21:13","moonPhase":"蛾眉月","moonPhaseIcon":"801","tempMax":"37","tempMin":"24","iconDay":"101","textDay":"多云","iconNight":"151","textNight":"多云","wind360Day":"45","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"16","wind360Night":"0","windDirNight":"北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"52","precip":"1.4","pressure":"985","vis":"25","cloud":"57","uvIndex":"4"},{"fxDate":"2025-07-28","sunrise":"05:10","sunset":"19:33","moonrise":"08:36","moonset":"21:34","moonPhase":"蛾眉月","moonPhaseIcon":"801","tempMax":"33","tempMin":"24","iconDay":"104","textDay":"阴","iconNight":"151","textNight":"多云","wind360Day":"315","windDirDay":"西北风","windScaleDay":"1-3","windSpeedDay":"3","wind360Night":"45","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"3","humidity":"62","precip":"10.5","pressure":"989","vis":"25","cloud":"66","uvIndex":"2"},{"fxDate":"2025-07-29","sunrise":"05:11","sunset":"19:33","moonrise":"09:39","moonset":"21:54","moonPhase":"蛾眉月","moonPhaseIcon":"801","tempMax":"30","tempMin":"24","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"321","windDirDay":"西北风","windScaleDay":"1-3","windSpeedDay":"15","wind360Night":"311","windDirNight":"西北风","windScaleNight":"1-3","windSpeedNight":"13","humidity":"50","precip":"0.0","pressure":"991","vis":"25","cloud":"10","uvIndex":"10"},{"fxDate":"2025-07-30","sunrise":"05:11","sunset":"19:32","moonrise":"10:40","moonset":"22:13","moonPhase":"蛾眉月","moonPhaseIcon":"801","tempMax":"32","tempMin":"24","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"354","windDirDay":"北风","windScaleDay":"3-4","windSpeedDay":"20","wind360Night":"339","windDirNight":"西北风","windScaleNight":"1-3","windSpeedNight":"17","humidity":"53","precip":"0.0","pressure":"993","vis":"25","cloud":"10","uvIndex":"10"},{"fxDate":"2025-07-31","sunrise":"05:12","sunset":"19:31","moonrise":"11:40","moonset":"22:34","moonPhase":"蛾眉月","moonPhaseIcon":"801","tempMax":"31","tempMin":"23","iconDay":"100","textDay":"晴","iconNight":"153","textNight":"晴间多云","wind360Day":"24","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"4","wind360Night":"171","windDirNight":"南风","windScaleNight":"1-3","windSpeedNight":"6","humidity":"70","precip":"0.0","pressure":"996","vis":"22","cloud":"5","uvIndex":"10"},{"fxDate":"2025-08-01","sunrise":"05:13","sunset":"19:30","moonrise":"12:41","moonset":"22:58","moonPhase":"上弦月","moonPhaseIcon":"802","tempMax":"32","tempMin":"23","iconDay":"101","textDay":"多云","iconNight":"151","textNight":"多云","wind360Day":"184","windDirDay":"南风","windScaleDay":"1-3","windSpeedDay":"7","wind360Night":"148","windDirNight":"东南风","windScaleNight":"1-3","windSpeedNight":"11","humidity":"69","precip":"0.0","pressure":"994","vis":"22","cloud":"25","uvIndex":"9"},{"fxDate":"2025-08-02","sunrise":"05:14","sunset":"19:29","moonrise":"13:43","moonset":"23:24","moonPhase":"盈凸月","moonPhaseIcon":"803","tempMax":"32","tempMin":"24","iconDay":"103","textDay":"晴间多云","iconNight":"151","textNight":"多云","wind360Day":"90","windDirDay":"东风","windScaleDay":"1-3","windSpeedDay":"6","wind360Night":"123","windDirNight":"东南风","windScaleNight":"1-3","windSpeedNight":"6","humidity":"70","precip":"0.0","pressure":"993","vis":"23","cloud":"25","uvIndex":"3"},{"fxDate":"2025-08-03","sunrise":"05:15","sunset":"19:27","moonrise":"14:45","moonset":"23:59","moonPhase":"盈凸月","moonPhaseIcon":"803","tempMax":"31","tempMin":"24","iconDay":"103","textDay":"晴间多云","iconNight":"302","textNight":"雷阵雨","wind360Day":"96","windDirDay":"东风","windScaleDay":"1-3","windSpeedDay":"2","wind360Night":"125","windDirNight":"东南风","windScaleNight":"1-3","windSpeedNight":"2","humidity":"68","precip":"0.5","pressure":"993","vis":"23","cloud":"39","uvIndex":"9"},{"fxDate":"2025-08-04","sunrise":"05:16","sunset":"19:26","moonrise":"15:46","moonset":"","moonPhase":"盈凸月","moonPhaseIcon":"803","tempMax":"30","tempMin":"23","iconDay":"300","textDay":"阵雨","iconNight":"153","textNight":"晴间多云","wind360Day":"25","windDirDay":"东北风","windScaleDay":"3-4","windSpeedDay":"20","wind360Night":"14","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"19","humidity":"69","precip":"8.5","pressure":"993","vis":"23","cloud":"69","uvIndex":"4"},{"fxDate":"2025-08-05","sunrise":"05:17","sunset":"19:25","moonrise":"16:43","moonset":"00:40","moonPhase":"盈凸月","moonPhaseIcon":"803","tempMax":"29","tempMin":"22","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"16","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"7","wind360Night":"156","windDirNight":"东南风","windScaleNight":"1-3","windSpeedNight":"4","humidity":"71","precip":"0.0","pressure":"994","vis":"23","cloud":"25","uvIndex":"9"},{"fxDate":"2025-08-06","sunrise":"05:18","sunset":"19:24","moonrise":"17:34","moonset":"01:29","moonPhase":"盈凸月","moonPhaseIcon":"803","tempMax":"30","tempMin":"23","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"56","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"7","wind360Night":"69","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"7","humidity":"71","precip":"0.6","pressure":"995","vis":"22","cloud":"56","uvIndex":"9"},{"fxDate":"2025-08-07","sunrise":"05:19","sunset":"19:23","moonrise":"18:18","moonset":"02:28","moonPhase":"盈凸月","moonPhaseIcon":"803","tempMax":"30","tempMin":"23","iconDay":"100","textDay":"晴","iconNight":"151","textNight":"多云","wind360Day":"49","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"7","wind360Night":"156","windDirNight":"东南风","windScaleNight":"1-3","windSpeedNight":"13","humidity":"71","precip":"0.0","pressure":"994","vis":"22","cloud":"10","uvIndex":"9"},{"fxDate":"2025-08-08","sunrise":"05:20","sunset":"19:22","moonrise":"18:55","moonset":"03:34","moonPhase":"盈凸月","moonPhaseIcon":"803","tempMax":"30","tempMin":"22","iconDay":"302","textDay":"雷阵雨","iconNight":"153","textNight":"晴间多云","wind360Day":"175","windDirDay":"南风","windScaleDay":"1-3","windSpeedDay":"7","wind360Night":"235","windDirNight":"西南风","windScaleNight":"1-3","windSpeedNight":"7","humidity":"68","precip":"1.8","pressure":"994","vis":"22","cloud":"64","uvIndex":"3"},{"fxDate":"2025-08-09","sunrise":"05:21","sunset":"19:21","moonrise":"19:26","moonset":"04:44","moonPhase":"满月","moonPhaseIcon":"804","tempMax":"28","tempMin":"22","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"293","windDirDay":"西北风","windScaleDay":"3-4","windSpeedDay":"28","wind360Night":"316","windDirNight":"西北风","windScaleNight":"3-4","windSpeedNight":"28","humidity":"70","precip":"0.0","pressure":"994","vis":"23","cloud":"20","uvIndex":"9"},{"fxDate":"2025-08-10","sunrise":"05:22","sunset":"19:19","moonrise":"19:54","moonset":"05:55","moonPhase":"亏凸月","moonPhaseIcon":"805","tempMax":"29","tempMin":"21","iconDay":"101","textDay":"多云","iconNight":"153","textNight":"晴间多云","wind360Day":"353","windDirDay":"北风","windScaleDay":"4-5","windSpeedDay":"32","wind360Night":"360","windDirNight":"北风","windScaleNight":"4-5","windSpeedNight":"32","humidity":"70","precip":"0.0","pressure":"996","vis":"24","cloud":"25","uvIndex":"6"},{"fxDate":"2025-08-11","sunrise":"05:23","sunset":"19:18","moonrise":"20:19","moonset":"07:06","moonPhase":"亏凸月","moonPhaseIcon":"805","tempMax":"28","tempMin":"20","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"11","windDirDay":"北风","windScaleDay":"3-4","windSpeedDay":"20","wind360Night":"46","windDirNight":"东北风","windScaleNight":"1-3","windSpeedNight":"17","humidity":"70","precip":"0.0","pressure":"994","vis":"24","cloud":"20","uvIndex":"9"},{"fxDate":"2025-08-12","sunrise":"05:24","sunset":"19:17","moonrise":"20:44","moonset":"08:17","moonPhase":"亏凸月","moonPhaseIcon":"805","tempMax":"28","tempMin":"20","iconDay":"100","textDay":"晴","iconNight":"150","textNight":"晴","wind360Day":"52","windDirDay":"东北风","windScaleDay":"1-3","windSpeedDay":"6","wind360Night":"174","windDirNight":"南风","windScaleNight":"1-3","windSpeedNight":"7","humidity":"70","precip":"0.0","pressure":"993","vis":"24","cloud":"5","uvIndex":"9"}],"refer":{"sources":["QWeather"],"license":["QWeather Developers License"]}}
    else:
        url = f"https://{API_HOST}/v7/weather/3d?location={location}&key={API_KEY}"
        response = requests.get(url)
        if response.status_code == 200:
            return json.loads(response.json())
        else:
            print(f"Error fetching data for location {location}: {response.status_code}")
            return None

def generate_day_template(daily_item):
    """根据daily项生成day模板"""
    # 读取day模板文件
    day_template = read_template_file("day-template.xaml")
    
    day_template_filled = day_template
    # 填充模板
    day_template_filled = day_template_filled.replace("{{url}}", GITURL)
    day_template_filled = day_template_filled.replace("{{fxDate}}", daily_item.get("fxDate", ""))
    day_template_filled = day_template_filled.replace("{{iconDay}}", daily_item.get("iconDay", ""))
    day_template_filled = day_template_filled.replace("{{textDay}}", daily_item.get("textDay", ""))
    day_template_filled = day_template_filled.replace("{{iconNight}}", daily_item.get("iconNight", ""))
    day_template_filled = day_template_filled.replace("{{textNight}}", daily_item.get("textNight", ""))
    day_template_filled = day_template_filled.replace("{{tempMax}}", daily_item.get("tempMax", ""))
    day_template_filled = day_template_filled.replace("{{tempMin}}", daily_item.get("tempMin", ""))
    day_template_filled = day_template_filled.replace("{{windSpeedDay}}", daily_item.get("windSpeedDay", ""))
    day_template_filled = day_template_filled.replace("{{wind360Day}}", daily_item.get("wind360Day", ""))
    day_template_filled = day_template_filled.replace("{{windSpeedNight}}", daily_item.get("windSpeedNight", ""))
    day_template_filled = day_template_filled.replace("{{wind360Night}}", daily_item.get("wind360Night", ""))
    day_template_filled = day_template_filled.replace("{{precip}}", daily_item.get("precip", ""))
    day_template_filled = day_template_filled.replace("{{vis}}", daily_item.get("vis", ""))
    day_template_filled = day_template_filled.replace("{{humidity}}", daily_item.get("humidity", ""))
    day_template_filled = day_template_filled.replace("{{uvIndex}}", daily_item.get("uvIndex", ""))
    day_template_filled = day_template_filled.replace("{{pressure}}", daily_item.get("pressure", ""))
    day_template_filled = day_template_filled.replace("{{cloud}}", daily_item.get("cloud", ""))
    
    return day_template_filled

def generate_city_files(city_name, daily_data, weather_data):
    """生成完整的城市XAML和JSON文件"""
    # 从daily数据中提取第一天的数据用于顶部显示
    first_day = daily_data[0]
    
    # 读取city模板文件
    city_template = read_template_file("city-template.xaml")
    
    # 生成day模板部分
    day_templates = ""
    for daily_item in daily_data:
        day_templates += generate_day_template(daily_item)
    
    # 填充城市模板
    city_xaml = city_template
    city_xaml = city_xaml.replace("{{GITURL}}", GITURL)
    city_xaml = city_xaml.replace("{{sitelink}}", weather_data.get("fxLink", ""))
    city_xaml = city_xaml.replace("{{name}}", city_name)
    city_xaml = city_xaml.replace("{{today}}", first_day.get("fxDate", ""))
    city_xaml = city_xaml.replace("{{fxDate}}", first_day.get("fxDate", ""))
    city_xaml = city_xaml.replace("{{iconDay}}", first_day.get("iconDay", ""))
    city_xaml = city_xaml.replace("{{textDay}}", first_day.get("textDay", ""))
    city_xaml = city_xaml.replace("{{iconNight}}", first_day.get("iconNight", ""))
    city_xaml = city_xaml.replace("{{textNight}}", first_day.get("textNight", ""))
    city_xaml = city_xaml.replace("{{tempMax}}", first_day.get("tempMax", ""))
    city_xaml = city_xaml.replace("{{tempMin}}", first_day.get("tempMin", ""))
    city_xaml = city_xaml.replace("{{windSpeedDay}}", first_day.get("windSpeedDay", ""))
    city_xaml = city_xaml.replace("{{wind360Day}}", first_day.get("wind360Day", ""))
    city_xaml = city_xaml.replace("{{windDirDay}}", first_day.get("windDirDay", ""))
    city_xaml = city_xaml.replace("{{windScaleDay}}", first_day.get("windScaleDay", ""))
    city_xaml = city_xaml.replace("{{windSpeedNight}}", first_day.get("windSpeedNight", ""))
    city_xaml = city_xaml.replace("{{wind360Night}}", first_day.get("wind360Night", ""))
    city_xaml = city_xaml.replace("{{windDirNight}}", first_day.get("windDirNight", ""))
    city_xaml = city_xaml.replace("{{windScaleNight}}", first_day.get("windScaleNight", ""))
    city_xaml = city_xaml.replace("{{sunrise}}", first_day.get("sunrise", ""))
    city_xaml = city_xaml.replace("{{sunset}}", first_day.get("sunset", ""))
    city_xaml = city_xaml.replace("{{moonrise}}", first_day.get("moonrise", ""))
    city_xaml = city_xaml.replace("{{moonset}}", first_day.get("moonset", ""))
    city_xaml = city_xaml.replace("{{precip}}", first_day.get("precip", ""))
    city_xaml = city_xaml.replace("{{vis}}", first_day.get("vis", ""))
    city_xaml = city_xaml.replace("{{humidity}}", first_day.get("humidity", ""))
    city_xaml = city_xaml.replace("{{uvIndex}}", first_day.get("uvIndex", ""))
    city_xaml = city_xaml.replace("{{pressure}}", first_day.get("pressure", ""))
    city_xaml = city_xaml.replace("{{cloud}}", first_day.get("cloud", ""))
    
    # 保存XAML文件
    xaml_filename = f"city/{city_name}.xaml"
    with open(xaml_filename, "w", encoding="utf-8") as f:
        f.write(city_xaml)
    
    # 创建配置文件
    timestamp = int(time.time())
    ini_filename = f"city/{city_name}.xaml.ini"
    with open(ini_filename, "w", encoding="utf-8") as f:
        f.write(str(timestamp))

    # 保存XAML文件到weather-forecast目录
    xaml_filename = f"weather-forecast/{city_name}.xaml"
    with open(xaml_filename, "w", encoding="utf-8") as f:
        f.write(city_xaml)
    
    # 创建JSON文件
    json_data = {"Title": f"{city_name} | 天气预报"}
    json_filename = f"weather-forecast/{city_name}.json"
    with open(json_filename, "w", encoding="utf-8") as f:
        json.dump(json_data, f, ensure_ascii=False, indent=4)
    
    print(f"已创建: {city_name} 的相关文件")

def main():
    """主函数"""
    # 从citydata.json加载城市数据
    city_data = load_city_data()
    
    # 处理每个城市
    for city in city_data:
        city_name = city["name"]
        location = city["num"]
        
        print(f"处理城市：{city_name} (编号：{location})")
        
        # 获取天气数据
        weather_data = fetch_weather_data(location)
        if weather_data and "daily" in weather_data:
            daily_data = weather_data["daily"]
            
            # 生成XAML和JSON文件
            generate_city_files(city_name, daily_data, weather_data)
        else:
            print(f"获取{city_name}天气数据失败")

if __name__ == "__main__":
    main()